package api

import (
	"api_getway/basic/config"
	__ "api_getway/basic/proto"
	"api_getway/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func GetType(c *gin.Context) {
	// Contact the server and print out its response.
	//ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	//defer cancel()
	//r, err := c.SayHello(ctx, &pb.HelloRequest{Name: *name})
	//if err != nil {
	//	log.Fatalf("could not greet: %v", err)
	//}
	//log.Printf("Greeting: %s", r.GetMessage())
}

func CreateOrder(c *gin.Context) {
	var req request.CreateOrderReq

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	created, err := config.MemberClient.CreateOrder(c, &__.CreateOrderReq{
		UserId:       req.UserId,
		MemberTypeId: req.MemberTypeId,
		Num:          req.Num,
		PayType:      req.PayType,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":     "订单创建成功",
		"code":    200,
		"orderSn": created.OrderSn,
		"PayUrl":  created.PayUrl,
	})
}

func ListOrderByUserId(c *gin.Context) {
	var req request.ListOrderReq

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	list, err := config.MemberClient.ListOrderByUserId(c, &__.ListOrderByUserIdReq{
		UserId: req.UserId,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":  "订单查询成功",
		"code": 200,
		"data": list,
	})
}
